﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Zhp.Common.Helper
{
    public static class ControllerHelper
    {
        public static TypeInfo[] GetAllControllers(IServiceCollection services)
        {
            var manager = services.BuildServiceProvider().GetService<ApplicationPartManager>();
            var feature = new ControllerFeature();
            manager.PopulateFeature(feature);

            var controllers = feature.Controllers
                .ToArray();

            return controllers;
        }

        public static TypeInfo[] GetAllControllers(IApplicationBuilder app)
        {
            var manager = app.ApplicationServices.GetService<ApplicationPartManager>();
            var feature = new ControllerFeature();
            manager.PopulateFeature(feature);

            var controllers = feature.Controllers
                .ToArray();

            return controllers;
        }
    }
}
